using System;
using System.Web.UI.WebControls;
using TrainGame.Model;
using TrainGame.GlobalState;

namespace _1856.UI
{
	/// <summary>
	/// Summary description for BuyNewTrain.
	/// </summary>
	public partial class BuyNewTrain : System.Web.UI.Page
	{
	
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void Page_Load(object sender, System.EventArgs e)
		{
			ErrorMsg.Text = "";
			
			FillTableWithTrainInfo();
		}


		private void FillTableWithTrainInfo()
		{
			Game	game = Global.getGame (this);;

			int				nQuantity		= 0;
			int				nPrevQuantity	= 0;
			bool			bEnabled;
			Button			button;
			TableCell		tableCell;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			CompanyMoney.Text = company.Name + " has $" + company.Cash;
				
			// set info for 2 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_2"); 
			nPrevQuantity	= nQuantity;
			nQuantity		= game.Trains_2.Count;
			tableCell.Text	= nQuantity.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_2"); 
			button			= new Button();
			button.Text		= "Buy";
			bEnabled		= nQuantity > 0 && nPrevQuantity == 0;
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.BuyTrain);
			button.CommandArgument = "2";
			tableCell.Controls.Add(button);

			if (bEnabled)
				game.CostOfNextNewTrainAvailable = game.Trains_2[0].Cost;
				
			// set info for 3 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_3"); 
			nPrevQuantity	= nQuantity;
			nQuantity		= game.Trains_3.Count;
			tableCell.Text	= nQuantity.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_3"); 
			button			= new Button();
			button.Text		= "Buy";
			bEnabled		= nQuantity > 0 && nPrevQuantity == 0;
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.BuyTrain);
			button.CommandArgument = "3";
			tableCell.Controls.Add(button);

			if (bEnabled)
				game.CostOfNextNewTrainAvailable = game.Trains_3[0].Cost;
			
			// set info for 4 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_4"); 
			nPrevQuantity	= nQuantity;
			nQuantity		= game.Trains_4.Count;
			tableCell.Text	= nQuantity.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_4"); 
			button			= new Button();
			button.Text		= "Buy";
			bEnabled		= nQuantity > 0 && nPrevQuantity == 0;
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.BuyTrain);
			button.CommandArgument = "4";
			tableCell.Controls.Add(button);

			if (bEnabled)
				game.CostOfNextNewTrainAvailable = game.Trains_4[0].Cost;
			
			// set info for 5 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_5"); 
			nPrevQuantity	= nQuantity;
			nQuantity		= game.Trains_5.Count;
			tableCell.Text	= nQuantity.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_5"); 
			button			= new Button();
			button.Text		= "Buy";
			bEnabled		= nQuantity > 0 && nPrevQuantity == 0;
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.BuyTrain);
			button.CommandArgument = "5";
			tableCell.Controls.Add(button);

			if (bEnabled)
				game.CostOfNextNewTrainAvailable = game.Trains_5[0].Cost;
			
			// set info for 6 train
			tableCell		= (TableCell) TrainTable.FindControl("Quanity_6"); 
			nPrevQuantity	= nQuantity;
			nQuantity		= game.Trains_6.Count;
			tableCell.Text	= nQuantity.ToString();
			
			tableCell		= (TableCell) TrainTable.FindControl("Buy_6"); 
			button			= new Button();
			button.Text		= "Buy";
			bEnabled		= nQuantity > 0 && nPrevQuantity == 0;
			button.Enabled	= bEnabled;
			button.Click	+= new System.EventHandler(this.BuyTrain);
			button.CommandArgument = "6";
			tableCell.Controls.Add(button);

			if (bEnabled)
				game.CostOfNextNewTrainAvailable = game.Trains_6[0].Cost;
			
            // do only 8 or D but not both and hide the non-appropriate row and column
			if (game.MaxTrain8Train)
			{
				// set info for 8 train
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_8"); 
				nPrevQuantity	= nQuantity;
				nQuantity		= game.Trains_8.Count;
				tableCell.Text	= nQuantity.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_8"); 
				button			= new Button();
				button.Text		= "Buy";
				bEnabled		= nQuantity > 0 && nPrevQuantity < 2;  // this is a slight hack...can buy an 8/D after first 6 bought (only 2 6's)
				button.Enabled	= bEnabled;
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "8";
				tableCell.Controls.Add(button);

				if (bEnabled)
					game.CostOfNextNewTrainAvailable = game.Trains_8[0].Cost;
			
				// hide the cells and row for the D train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[5].Visible = false;

				TrainTable.Rows[7].Visible = false;
			}
			else
			{
				// set info for D train
				tableCell		= (TableCell) TrainTable.FindControl("Quanity_D"); 
				nPrevQuantity	= nQuantity;
				nQuantity		= game.Trains_D.Count;
				tableCell.Text	= nQuantity.ToString();
			
				tableCell		= (TableCell) TrainTable.FindControl("Buy_D"); 
				button			= new Button();
				button.Text		= "Buy";
                bEnabled        = nQuantity > 0 && nPrevQuantity < 2;  // this is a slight hack...can buy an 8/D after first 6 bought  (only 2 6's)
                button.Enabled  = bEnabled;
				button.Click	+= new System.EventHandler(this.BuyTrain);
				button.CommandArgument = "D";
				tableCell.Controls.Add(button);

				if (bEnabled)
					game.CostOfNextNewTrainAvailable = game.Trains_D[0].Cost;
			
				// hide the cells and row for the 8 train
				for (int i = 0; i < TrainTable.Rows.Count; i++)
					TrainTable.Rows[i].Cells[4].Visible = false;
				
				TrainTable.Rows[6].Visible = false;
			}
		}
		

		private void BuyTrain(object sender, System.EventArgs e)
		{
			Game			game	= Global.getGame (this);;
			string			sTrain;
			Button			button	= (Button) sender;
			PublicCompany	company;

			company = game.CurrentlyOperatingCompany;
			sTrain	= button.CommandArgument;

			if (company != null)
			{
				string sErrorMsg;

				if (company.BuyTrain(sTrain, out sErrorMsg))
				{
					string sMsg;
					
					sMsg = company.Name + " bought a " + sTrain + " train";
					game.History.Add (sMsg);
					
					Response.Redirect("BuyNewTrain.aspx");
				}
				else
				{
					ErrorMsg.Text = sErrorMsg;
				}
			}
		}

		protected void Back_Click(object sender, System.EventArgs e)
		{
			Response.Redirect("BuyTrain.aspx");
		}

	}
}
